package com.cyms.modules.card.service.impl;

import com.cyms.modules.base.entity.CompanyEntity;
import com.cyms.modules.base.service.CompanyService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.Query;
import com.cyms.modules.base.entity.CarEntity;
import com.cyms.modules.base.service.CarService;
import com.cyms.modules.card.dao.EtcDao;
import com.cyms.modules.card.entity.EtcEntity;
import com.cyms.modules.card.service.EtcService;


@Service("etcService")
public class EtcServiceImpl extends ServiceImpl<EtcDao, EtcEntity> implements EtcService {
	@Autowired
    private CarService carService;
    @Autowired
    private CompanyService companyService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    	String code=(String) params.get("code");
    	Long createOrg = (Long) params.get("createOrg");
        List<CompanyEntity> companyList = companyService.selectList(new EntityWrapper<CompanyEntity>().eq("create_org",createOrg));
        if (companyList.size() == 0) {
            return new PageUtils(new Page<>());
        }
        Page<EtcEntity> page = this.selectPage(
                new Query<EtcEntity>(params).getPage(),
                new EntityWrapper<EtcEntity>()
                        .orderBy("create_date", false)
                        .like(StringUtils.isNotBlank(code),"code",code)
                        .and()
                        .in(companyList != null && companyList.size() > 0,"company_id", companyList.parallelStream().map((c)->c.getId()).collect(Collectors.toList()))
        );
        page.setRecords(page.getRecords().parallelStream().map(etc->{
        	CarEntity car=carService.selectById(etc.getCarId());
        	etc.setCar(car);
        	return etc;
        }).collect(Collectors.toList()));
        return new PageUtils(page);
    }

}
